<?php

namespace Abzu\InitBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Abzu\InitBundle\Entity\AzSetting;
use Abzu\InitBundle\Form\AzSettingType;

class AzSettingsController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $azSettings = $em->getRepository('AbzuInitBundle:AzSetting')->findAll();
        
        return $this->render('AbzuInitBundle:AzSettings:index.html.twig', array(
            'azSettings' => $azSettings
        ));
    }

    public function newAction() 
    {
        $azSetting = new \Abzu\InitBundle\Entity\AzSetting();
        $form = $this->createForm(new \Abzu\InitBundle\Form\AzSettingType(), $azSetting);
        
        return $this->render('AbzuInitBundle:AzSettings:new.html.twig', 
                array(
                    'form' => $form->createView(),
                    'azSetting' => $azSetting
                ));
    }

    public function createAction() {
        $azSetting = new \Abzu\InitBundle\Entity\AzSetting();
        $form = $this->createForm(new \Abzu\InitBundle\Form\AzSettingType(), $azSetting);
        $form->bindRequest($this->getRequest());
        if ($form->isValid())
        {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($azSetting);
            $em->flush();
            $this->get('session')->setFlash('notice', 'El setting fue guardado correctamente');
            $this->redirect($this->generateUrl(
                    'AbzuInitBundle_AzSettings_edit', array('id' => $azSetting->getId())
                    ));
        }
        return $this->render('AbzuInitBundle:AzSettings:new.html.twig', array(
            'form' => $form->createView(), 'azSetting' => $azSetting
        ));
    }

    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $azSetting = $em->getRepository('AbzuInitBundle:AzSetting')->find($id);
        $form = $this->createForm(new \Abzu\InitBundle\Form\AzSettingType(), $azSetting);
        $form->bindRequest($this->getRequest());
        if ($form->isValid())
        {
            $em->persist($azSetting);
            $em->flush();
            $this->get('session')->setFlash('notice', 'The settings was successfully updated.');
        }
        return $this->render('AbzuInitBundle:AzSettings:edit.html.twig', array(
            'form' => $form->createView(), 'azSetting' => $azSetting
        ));
        
    }

    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $azSetting = $em->getRepository('AbzuInitBundle:AzSetting')->find($id);
        if (!$azSetting)
            throw $this->createNotFoundException ('No found the setting #'.$id.' ');
        $form = $this->createForm(new \Abzu\InitBundle\Form\AzSettingType(), $azSetting);
        
        return $this->render('AbzuInitBundle:AzSettings:edit.html.twig', array(
            'form' => $form->createView(), 'azSetting' => $azSetting 
        ));
    }

    
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $azSetting = $em->getRepository('AbzuInitBundle:AzSetting')->find($id);
        if (!$azSetting)
            throw $this->createNotFoundException ('The setting #'.$id.' don\'t exists');
        
        return $this->render('AbzuInitBundle:AzSettings:show.html.twig', array('azSetting' => $azSetting ));
    }

}
